<template >
  <el-container>
    <el-header>
      <div>
        <span class="text">电商后台管理系统</span>
      </div>

      <el-button type="info" @click="quit">退出</el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <!-- 侧边栏随着折叠而折叠，如果btnsCollapse是false侧边栏为64px否则为200px -->
      <el-aside :width="btnsCollapse ?'64px':'200px'">
        -
        <div class="button" @click="btns">|||</div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="lightblue"
          unique-opened
          :collapse="btnsCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 -->
          <!-- :index="item.id"会报错，因为index只接收字符串，而item.id是数值，所以要拼接一个字符串，数值+字符串=字符串 -->
          <!-- index前面一定要:号这很重要属性绑定作用 -->
          <el-submenu :index="item.id+''" v-for="item in menulist" :key="item.id">
            <template slot="title">
              <!-- <i class="el-icon-location"></i> -->
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item-group>
              <!-- index前面一定要:号这很重要绑定作用 -->
              <!-- path路径前面一定要/号这很重要 -->
              <el-menu-item
                :index="'/'+item2.path"
                v-for="item2 in item.children"
                :key="item2.id"
                @click="saveNavState('/'+item2.path)"
              >
                <i class="el-icon-menu"></i>
                <span>{{item2.authName}}</span>
              </el-menu-item>
            </el-menu-item-group>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容主体 -->
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menulist: [],
      btnsCollapse: false,
      activePath: ""
    };
  },
  created() {
    this.getMenuList();
    this.activePath = window.sessionStorage.getItem("activePath");
  },
  methods: {
    quit() {
      window.sessionStorage.clear();
      this.$router.push("/login");
    },
    // 获取所有菜单
    async getMenuList() {
      const { data: res } = await this.$http.get("menus");
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg);
      } else {
        this.menulist = res.data;
        console.log(res);
      }
    },
    //点击按钮，切换菜单的折叠与展开
    btns() {
      this.btnsCollapse = !this.btnsCollapse;
    },
    //把选择的地址状态保存到sessionStorage,页面加载完就赋予点击对象的地址
    saveNavState(activePath) {
      window.sessionStorage.setItem("activePath", activePath);
      this.activePath = activePath;
    }
  }
};
</script>

<style lang="less" scoped>
//这里lang里面不写less的话css是不会生效的
.el-header {
  background-color: #373d41;
  display: flex;
  justify-content: space-between;
  align-items: center;
  img {
    width: 50px;
    height: 50px;
  }
  padding: 0px;

  font-size: 30px;

  color: white;
  div {
    display: flex;
    align-items: center;
  }
}

.el-aside {
  background-color: #333744;
}
.el-main {
  background-color: #eaedf1;
}
.el-container {
  height: 100%;
}
.el-menu {
  //消除侧边栏凸出的盒子边框实线
  border-right: solid 0px;
}
.button {
  text-align: center;
  color: white;
}
</style>